package com.gaoyi.crm.util;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.Log;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.view.TimePickerView;
import com.gaoyi.crm.R;
import com.gaoyi.crm.constant.Constants;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
    public static String DateTimeToStr(Date date, String format) {
        //可根据需要自行截取数据显示
        if (date == null)
            return "";
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format, LanguageUtils.getInstance().getCurrentLanguage());
            return simpleDateFormat.format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    public static Date StrToDateTime(String str, String format) {
        if (TextUtils.isEmpty(str)) {
            return null;
        }
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format, LanguageUtils.getInstance().getCurrentLanguage());
            return simpleDateFormat.parse(str);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void showDatePicker(Context context, String format, Calendar selectDate, ShowTimeSelectorCallback callback) {
        if (TextUtils.isEmpty(format)) {
            Log.e(Constants.TAG, "showDatePicker: no format");
            return;
        }
        TimePickerBuilder builder = new TimePickerBuilder(context, (date, v) -> {
            if (callback != null) {
                callback.OnTimeSelectListener(date);
            }
        });
        Calendar startCalendar = Calendar.getInstance();
        startCalendar.set(2000, 1, 1);
        Calendar calendarEnd = Calendar.getInstance();
        calendarEnd.set(calendarEnd.get(Calendar.YEAR) + 10, 12, 31);
        TimePickerView pickerView = builder.setSubmitText(context.getString(R.string.ok))
                .setTitleText(context.getString(R.string.p_sel))
                .setSubmitColor(context.getResources().getColor(R.color.purple))
                .setCancelText(context.getString(R.string.cancel))
                .setDate(selectDate)
                .setRangDate(startCalendar, calendarEnd)
                .isCenterLabel(true)
                .setDividerColor(Color.DKGRAY)
                .setLineSpacingMultiplier(3.0f)
                .setItemVisibleCount(7)
                .setType(new boolean[]{format.contains("yyyy"),
                        format.contains("MM"),
                        format.contains("dd"),
                        format.contains("HH"),
                        format.contains("mm"),
                        format.contains("ss")})
                .setLabel(format.contains("yyyy") ? context.getString(R.string.year) : "",
                        format.contains("MM") ? context.getString(R.string.month) : "",
                        format.contains("dd") ? context.getString(R.string.day) : "",
                        format.contains("HH") ? context.getString(R.string.hours) : "",
                        format.contains("mm") ? context.getString(R.string.min) : "",
                        format.contains("ss") ? context.getString(R.string.seconds) : "")
                .build();
        pickerView.show();
    }

    public interface ShowTimeSelectorCallback {
        void OnTimeSelectListener(Date date);
    }

    public static int getDateDistance(Calendar begin, Calendar end) {
        return (int) ((end.getTimeInMillis() - begin.getTimeInMillis()) / (1000 * 3600 * 24)) + 1;
    }

    public static Date getFrontDate(Date date, int num) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int oldDay = calendar.get(Calendar.DAY_OF_MONTH);
        calendar.set(Calendar.DAY_OF_MONTH, oldDay - num + 1);
        return calendar.getTime();
    }
}
